// store/index.js
import {createStore} from 'vuex';


export default createStore({
  state: {
    theme: 'light',
    isCollapse: false,
    users: [
      { username: '田益',
        password: '111',
        url:"https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        phone:"18713442057"
      },
      { username: '玉子',
        password: '123',
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.1VaJB3jOIcNl3qrAMsQWBQHaHa?rs=1&pid=ImgDetMain",
        phone:"114514"
      },
      { username: 'x毛',
        password: '123',
        url: "https://media.fgo.wiki/1/1f/%E8%B0%9C%E4%B9%8B%E5%A5%B3%E4%B8%BB%E8%A7%92X-%E5%8D%A1%E9%9D%A2%E6%84%9A%E4%BA%BA%E8%8A%82.png",
        phone:"31415926"
      }
    ],
    activeUser: null,
    breadcrumbs: [],
    tags: [],
    activeTag: '', // 当前激活的标签路径
    userForm:[
      {
        id: 1,
        name: "admin_user",
        password: "admin123",
        otherName: "超级管理员",
        mail: "admin@example.com",
        phone: "13800138000",
        role: 1,
        del: false
      },
      {
        id: 2,
        name: "counselor_user",
        password: "counselor123",
        otherName: "专业咨询师",
        mail: "counselor@example.com",
        phone: "13900139000",
        role: 2,
        del: false
      },
      {
        id: 3,
        name: "normal_user",
        password: "normal123",
        otherName: "普通用户",
        mail: "user@example.com",
        phone: "13700137000",
        role: 3,
        del: false
      },
      {
        id: 4,
        name: "manager_user",
        password: "manager123",
        otherName: "部门经理",
        mail: "manager@example.com",
        phone: "13501350000",
        role: 1,
        del: false
      },
      {
        id: 5,
        name: "advisor_user",
        password: "advisor123",
        otherName: "资深顾问",
        mail: "advisor@example.com",
        phone: "13601360000",
        role: 2,
        del: false
      },
      {
        id: 6,
        name: "advisor_user",
        password: "advisor123",
        otherName: "资深顾问",
        mail: "advisor@example.com",
        phone: "13601360000",
        role: 2,
        del: false
      },
      {
        id: 7,
        name: "advisor_user",
        password: "advisor123",
        otherName: "资深顾问",
        mail: "advisor@example.com",
        phone: "13601360000",
        role: 2,
        del: false
      },
      {
        id: 8,
        name: "advisor_user",
        password: "advisor123",
        otherName: "资深顾问",
        mail: "advisor@example.com",
        phone: "13601360000",
        role: 2,
        del: false
      },
      {
        id: 9,
        name: "advisor_user",
        password: "advisor123",
        otherName: "资深顾问",
        mail: "advisor@example.com",
        phone: "13601360000",
        role: 2,
        del: false
      },
      {
        id: 10,
        name: "advisor_user",
        password: "advisor123",
        otherName: "资深顾问",
        mail: "advisor@example.com",
        phone: "13601360000",
        role: 2,
        del: false
      },
      {
        id: 11,
        name: "advisor_user",
        password: "advisor123",
        otherName: "资深顾问",
        mail: "advisor@example.com",
        phone: "13601360000",
        role: 2,
        del: false
      }
    ],
    userForm2:[
      {
        id: 1,
        name: "张伟",
        levelName: "资深咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "心理健康、职业规划",
        address: "北京市朝阳区",
        introduction: "拥有10年心理咨询经验，擅长解决个人成长和职业发展问题。",
        del: false
      },
      {
        id: 2,
        name: "李娜",
        levelName: "高级咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "婚姻家庭、亲子教育",
        address: "上海市浦东新区",
        introduction: "专注于家庭关系和儿童心理发展，提供专业的咨询服务。",
        del: false
      },
      {
        id: 3,
        name: "王强",
        levelName: "中级咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "情绪管理、压力调适",
        address: "广州市天河区",
        introduction: "擅长帮助客户理解和管理情绪，提高生活质量。",
        del: false
      },
      {
        id: 4,
        name: "赵敏",
        levelName: "初级咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "人际关系、沟通技巧",
        address: "深圳市南山区",
        introduction: "致力于提升人际交往能力，改善沟通技巧。",
        del: false
      },
      {
        id: 5,
        name: "孙涛",
        levelName: "资深咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "职业规划、时间管理",
        address: "杭州市西湖区",
        introduction: "拥有丰富的职业规划经验，帮助客户实现职业目标。",
        del: false
      },
      {
        id: 6,
        name: "周梅",
        levelName: "高级咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "个人成长、自我实现",
        address: "成都市武侯区",
        introduction: "专注于个人潜能开发，助力客户实现自我价值。",
        del: false
      },
      {
        id: 7,
        name: "吴刚",
        levelName: "中级咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "情绪调节、自我认知",
        address: "武汉市江岸区",
        introduction: "帮助客户认识自我，调节情绪，提升自我认知。",
        del: false
      },
      {
        id: 8,
        name: "郑洁",
        levelName: "初级咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "人际关系、团队协作",
        address: "南京市玄武区",
        introduction: "擅长改善人际关系和团队合作，提升团队效能。",
        del: false
      },
      {
        id: 9,
        name: "冯超",
        levelName: "资深咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "压力管理、危机干预",
        address: "西安市雁塔区",
        introduction: "在压力管理和危机干预方面有深入研究，提供专业的解决方案。",
        del: false
      },
      {
        id: 10,
        name: "陈静",
        levelName: "高级咨询师",
        url: "https://tse4-mm.cn.bing.net/th/id/OIP-C.fjqPsB4ZYrnGiQiC3Hu9FQHaHa?rs=1&pid=ImgDetMain",
        specialty: "职业发展、生涯规划",
        address: "重庆市渝中区",
        introduction: "专注于职业生涯规划，帮助客户实现职业发展。",
        del: false
      }
    ],
    consultantForm:[],//咨询师表单
    originalConsultantForm: [], // 原始咨询师列表
    SpecializationForm:[],
    OriginalSpecializationForm:[],
    SpecializationForm1:[],
    AppointmentForm:[],
    originalAppointmentForm:[]
  },
  mutations: {
    setTheme(state, newTheme) {
      state.theme = newTheme;
      if (newTheme === 'dark') {
        document.documentElement.classList.add('dark');
      } else {
        document.documentElement.classList.remove('dark');
      }
    },
    setActiveUser(state, user) {
      state.activeUser = user;
    },
    setCollapse(state) {//控制折叠
      state.isCollapse = !state.isCollapse;
    },
    setBreadcrumbs(state, records) {
      state.breadcrumbs = records.map((record) => ({
        name: record.meta.title,
        to: record.name ? `/${record.name.toLowerCase()}` : '/', // 确保路径是小写
      }));
    },
    setTitle2(state, title) {
      state.title2 = title;
    },
    addTag(state, tag) {
      const index = state.tags.findIndex(t => t.path === tag.path);
      if (index === -1) {
        state.tags.push(tag);
      }
    },
    removeTag(state, path) {
      state.tags = state.tags.filter(tag => tag.path !== path);
      if (state.activeTag === path) {
        state.activeTag = ''; // 移除的标签如果是激活的，重置激活状态
      }
    },
    setActiveTag(state, path) {
      state.activeTag = path;
    },
  //   ===================用户表单===============================
    setUserForm(state, filteredUsers) {
      state.userForm = filteredUsers;
    },
    addUser(state, newUser) {
      state.userForm.push(newUser);
    },
    removeUser(state, index) {
      state.userForm.splice(index, 1);
    },
    updateUser(state, {index, updatedUser}) {
      state.userForm[index] = {...state.userForm[index], ...updatedUser};
    },
    addUserForm2(state, newUser) {
      state.userForm2.push(newUser);
    },
    removeUserForm2(state, index) {
      state.userForm2.splice(index, 1);
    },
    updateUserForm2(state, { index, updatedUser }) {
      state.userForm2[index] = { ...state.userForm2[index], ...updatedUser };
    },


  // ======================田益的列表=====================================

    setConsultantForm(state, consultant) {
      state.consultantForm = consultant?.map(consultant => ({
        ...consultant,  id: consultant.id || null,
      })) || [];
    },
    setOriginalConsultantForm(state, consultant){
      state.originalConsultantForm = consultant ? [...consultant] : []; // 使用扩展运算符创建副本，并确保不会设置为 null
    },
    resetConsultantForm(state) {
      state.consultantForm = [...state.originalConsultantForm]; // 恢复原始用户列表
    },
    addConsultant(state, newConsultant) {
      // 添加新用户到当前列表
      state.consultantForm.push(newConsultant);
      // 同时更新原始列表
      state.originalConsultantForm.push(newConsultant);
    },
    deleteConsultant(state, id) {
      state.consultantForm = state.consultantForm.filter(consultant => consultant.id !== id);
      state.originalConsultantForm = state.originalConsultantForm.filter(consultant => consultant.id !== id);
    },
    updateConsultant(state, { id, updatedConsultant }) {
      const index = state.consultantForm.findIndex(consultant => consultant.id === id);
      if (index !== -1) {
        state.consultantForm[index] = { ...state.consultantForm[index], ...updatedConsultant };
        state.originalConsultantForm[index] = { ...state.originalConsultantForm[index], ...updatedConsultant };
      }
    },
    updateChargetype(state, { id, updatedChargetype }) {
      const index = state.consultantForm.findIndex(consultant => consultant.id === id);
      if (index !== -1) {
        state.consultantForm[index].chargetype = updatedChargetype;
        state.originalConsultantForm[index].chargetype = updatedChargetype;
      }
    },
    setSpecializationForm(state, Specialization){
      state.SpecializationForm = Specialization;
    },
    setOriginalSpecializationForm(state, Specialization){
      state.originalSpecializationForm = Specialization ? [...Specialization] : [];
    },
    resetSpecializationForm(state) {
      state.SpecializationForm = [...state.OriginalSpecializationForm];
    },
    setSpecialization(state, Specialization){
      state.SpecializationForm1 = Specialization;
    },
    addSpecialization(state, newSpecialization) {
      state.SpecializationForm.push(newSpecialization);
      state.OriginalSpecializationForm.push(newSpecialization);
    },
    deleteSpecialization(state, id) {
      state.SpecializationForm = state.SpecializationForm.filter(Specialization => Specialization.id !== id);
      state.originalSpecializationForm = state.originalSpecializationForm.filter(Specialization => Specialization.id !== id);
    },

    setAppointmentForm(state, appointment){
      state.AppointmentForm = appointment;
    },
    setOriginalAppointmentForm(state, appointment){
      state.originalAppointmentForm = appointment ? [...appointment] : [];
    },
    resetAppointmentForm(state) {
      state.AppointmentForm = [...state.originalAppointmentForm];
    },
    addAppointment(state, newAppointment) {
      state.AppointmentForm.push(newAppointment);
      state.originalAppointmentForm.push(newAppointment);
    },
    deleteAppointment(state, id) {
      state.AppointmentForm = state.AppointmentForm.filter(appointment => appointment.id !== id);
      state.originalAppointmentForm = state.originalAppointmentForm.filter(appointment => appointment.id !== id);
    },
    updateAppointment(state, { id, updatedAppointment }) {
      const index = state.AppointmentForm.findIndex(appointment => appointment.id === id);
      if (index !== -1) {
        state.AppointmentForm[index] = { ...state.AppointmentForm[index], ...updatedAppointment };
        state.originalAppointmentForm[index] = { ...state.originalAppointmentForm[index], ...updatedAppointment };
      }
    },




  // ======================田益的列表=====================================


  },
  actions: {
    toggleTheme({ commit }) {
      const newTheme = store.state.theme === 'light' ? 'dark' : 'light';
      commit('setTheme', newTheme);
    },
    addUserToForm2({ commit }, newUser) {
      commit('addUserForm2', newUser);
    },
    removeUserFromForm2({ commit }, index) {
      commit('removeUserForm2', index);
    },
    updateUserInForm2({ commit }, { index, updatedUser }) {
      commit('updateUserForm2', { index, updatedUser });
    },

  },
  modules: {
  }
});